package com.wen.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wen.common.R;
import com.wen.pojo.Gather;
import com.wen.pojo.dto.GatherDto;
import com.wen.pojo.vo.GatherVo;
import com.wen.service.GatherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/gather")
public class GatherController {
    @Autowired
    private GatherService service;

    @GetMapping("gatherAll")
    public R gatherAll(){
        List<Gather> gathers = service.GatherAll();
        return R.ok().data("gathers",gathers);
    }

        @PostMapping("/page/{pageNum}/{pageSize}")
    public R page(@RequestBody GatherVo gatherVo,@PathVariable Long pageNum,@PathVariable Long pageSize){
        Page<Gather> gatherPage = service.pageListGather(gatherVo, pageNum, pageSize);
        return R.ok().data("files",gatherPage.getRecords()).data("total",gatherPage.getTotal())
                .data("current",gatherPage.getCurrent()).data("size",gatherPage.getSize());
    }
    @GetMapping("gatherById/{id}")
    public R gatherById(@PathVariable String id){
        GatherDto gatherDto = service.gathById(id);
        return R.ok().data("gatherDto",gatherDto);
    }
    @GetMapping("gatherByIds/{id}")
    public R gatherByIds(@PathVariable String id){

        boolean flag = service.gatherByIds(id);
        if (flag)
        return R.ok().message("已拒绝请求");
        return R.error().message("拒绝请求失败");
    }


    @Autowired
    private PurchaseOrderService service1;

    @Autowired
    private GatherDetailsService service2;

    @PostMapping("GatherAdd/{storer}/{totalPrice}/{purchaseId}/{sid}")
    public R GatherAdd(@RequestBody List<PurchaseOrderDetailsSP> sp, @PathVariable String storer, @PathVariable String totalPrice, @PathVariable Long purchaseId,@PathVariable Long sid) {
        Gather gather = new Gather();
        GatherDetails gatherDetails = new GatherDetails();
        UpdateWrapper updateWrapper = new UpdateWrapper();
        System.out.println(storer);
        System.out.println(totalPrice);
        System.out.println(purchaseId);
        System.out.println(sp);
        float jg = 0;
        float jg1 = 0;
        for (PurchaseOrderDetailsSP p : sp) {
            System.out.println(p);
            jg += p.getCount2() * p.getPrice();
            gather.setTotalAmout(p.getCount2());
            gather.setTotalPrice(jg);
            gather.setPurchaseId(purchaseId);
            gather.setStorer(storer);
            gather.setReason("r001-1");
            gather.setStockId(p.getStockid());
            gather.setRegister(storer);
            gather.setRegisterTime(new Date());
            gather.setCheckTag("s001-1");
        }

        boolean save = service.save(gather);
        System.out.println("入库="+save);

        if (save) {
             for (PurchaseOrderDetailsSP p : sp) {
                 long gatherId = gather.getGatherId();
                 gatherDetails.setGatherId(gatherId);
                 gatherDetails.setProductId(p.getId());
                 gatherDetails.setSupplierId(sid);
                 gatherDetails.setAmount(p.getCount2());
                 jg1 = p.getCount2() * p.getPrice();
                 gatherDetails.setPrice(jg1);
                 gatherDetails.setSubTotal(jg);
             }
            boolean save1 = service2.save(gatherDetails);
            System.out.println("明细="+save1);

            updateWrapper.eq("purchase_id", purchaseId);

            updateWrapper.set("gather_tag", "k001-3");

            boolean update = service1.update(null, updateWrapper);
            System.out.println(update);
            return R.ok().message("添加成功").data("total","1");
        }
        return R.error().message("添加失败");
    }

}
